﻿// See https://aka.ms/new-console-template for more information

using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;


try
{
    var factory = new ConnectionFactory() { HostName = "192.168.139.121", UserName = "admin", Password = "admin" };
    using (var connection = factory.CreateConnection())
    using (var channel = connection.CreateModel())
    {
       // channel.ExchangeDeclare(exchange: "test", type: ExchangeType.Direct, true);

        var queueName = channel.QueueDeclare("test", true, false, true, null).QueueName;
        channel.QueueBind(queue: queueName,
            exchange: "test",
            routingKey: "eventName");

        Console.WriteLine(" [*] Waiting for test.");

        var consumer = new EventingBasicConsumer(channel);
        consumer.Received += (model, ea) =>
        {
            var body = ea.Body.ToArray();
            var message = Encoding.UTF8.GetString(body);
            Console.WriteLine(" [x] {0}", message);
        };
        channel.BasicConsume(queue: queueName,
            autoAck: true,
            consumer: consumer);

        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
    }
}
catch (Exception e)
{
    Console.WriteLine(e);

    Console.ReadLine();
}